RAIS  3.2
C:/Projekte/RAIS/FillCustomQueryTableCL/Parameter.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Data.SqlClient;
00006 using System.Xml;
00007 using RAIS.DataAccessLayer;
00008 namespace FillCustomQueryTableCL
00009 {
00010     public class Parameter
00011     {
00012         #region Constants
00013         
00014         public static readonly string START_DATE_NAME = "StartDate";
00015         public static readonly string END_DATE_NAME = "EndDate";
00016         public static readonly string USER_ID_NAME = "UserID";
00017 
00018         public static readonly string TABLE_ID_FIELD = "TableID";
00019         public static readonly string ORDER_FIELD = "Order";
00020         public static readonly string PARAM_NAME_FIELD = "ParamName";
00021         public static readonly string QUERY_NAME_FIELD = "QueryName";
00022         public static readonly string RELATION_QUERY_NAME = "RelationQueryName";
00023         public static readonly string VISIBLE_NAME_FIELD = "VisibleName";
00024 
00025         public static Dictionary<string, int> ParameterTypes = new Dictionary<string, int>();
00026 
00027         private static readonly string GetParameterTypes =
00028                                 "SELECT " +
00029                                 "    [PK Query Parameter Type ID], " +
00030                                 "    [Query Parameter Type Name] " +
00031                                 "FROM [Query Parameter Type] ";
00032         #endregion
00033 
00034 
00035         public static void InitParameter(string connectionString)
00036         {
00037                         if (ParameterTypes.Count == 0)
00038                         {
00039                                 using (SqlConnection sqlConnection = new SqlConnection(connectionString))
00040                                 {
00041                                         sqlConnection.Open();
00042                                         using (SqlCommand sqlCommand = new SqlCommand("", sqlConnection))
00043                                         {
00044                                                 sqlCommand.CommandText = GetParameterTypes;
00045                                                 using (SqlDataReader reader = sqlCommand.ExecuteReader())
00046                                                 {
00047                                                         while (reader.Read())
00048                                                                 ParameterTypes.Add((string)reader["Query Parameter Type Name"], (int)reader["PK Query Parameter Type ID"]);
00049                                                         ParameterTypes.Add("DateRange", 0xFFFFFFF);
00050                                                 }
00051                                         }
00052                                 }
00053                         }
00054         }
00055 
00056 
00057         #region Fields
00058         protected string name;
00059         protected string visibleName;
00060         protected int type;
00061         protected int tableID;
00062         protected string relatedQueryName;
00063         protected int order;
00064         protected string queryName;
00065         #endregion
00066 
00067         #region Properties
00068         public string TypeName
00069         {
00070             set { type = ParameterTypes[value]; }
00071         }
00072 
00073         public string QueryName
00074         {
00075             get { return queryName; }
00076             set { queryName = value; }
00077         }
00078         public string Name
00079         {
00080             get { return name; }
00081             set { name = value; }
00082         }
00083         public string VisibleName
00084         {
00085             get { return visibleName; }
00086             set { visibleName = value; }
00087         }
00088         public int Type
00089         {
00090             get { return type; }
00091             set { type = value; }
00092         }
00093         public int TableID
00094         {
00095             get { return tableID; }
00096             set { tableID = value; }
00097         }
00098         public string RelatedQueryName
00099         {
00100             get { return relatedQueryName; }
00101             set { relatedQueryName = value; }
00102         }
00103         public int Order
00104         {
00105             get { return order; }
00106             set { order = value; }
00107         }
00108         #endregion
00109 
00110         #region Constructors        
00111         public Parameter(string queryName, string name, string visibleName, int type, int tableID, string relatedQueryName, int order)
00112         {
00113             this.queryName = queryName;
00114             this.name = name;
00115             this.visibleName = visibleName;
00116             this.type = type;
00117             this.tableID = tableID;
00118             this.relatedQueryName = relatedQueryName;
00119             this.order = order;
00120         }
00121 
00122         public Parameter(string queryName, string name, string visibleName, string type, int tableID, string relatedQueryName, int order):
00123                 this(queryName, name, visibleName, ParameterTypes[type], tableID, relatedQueryName, order)
00124         {}
00125 
00126         #endregion
00127 
00128         #region Public methods
00129         public Parameter(SqlDataReader reader)
00130         {
00131             VisibleName = (string)reader[VISIBLE_NAME_FIELD];
00132             if ((string)reader[Parameter.RELATION_QUERY_NAME] == "")
00133                 relatedQueryName = null;
00134             else
00135                 relatedQueryName = (string)reader[Parameter.RELATION_QUERY_NAME];
00136 
00137             if (reader[TABLE_ID_FIELD] == DBNull.Value)
00138                 tableID = -1;
00139             else
00140                 tableID = (int)reader[TABLE_ID_FIELD];
00141 
00142             order = (int)reader[ORDER_FIELD];
00143             name = (string)reader[PARAM_NAME_FIELD];
00144 
00145             if (visibleName == "User")
00146             {
00147                 type = ParameterTypes["UserID"];
00148                 name = USER_ID_NAME;
00149             }
00150             else if (visibleName == "DatesRange")
00151                 type = ParameterTypes["DateRange"];
00152             else if (relatedQueryName == null)
00153                 type = ParameterTypes["LookupFromTable"];
00154             else
00155                 type = ParameterTypes["LookupFromQuery"];
00156         }
00157         public virtual Parameter Clone()
00158         {
00159             return new Parameter(queryName, name, visibleName, type, tableID, relatedQueryName, order);
00160         }
00161         public void WriteToXML(XmlWriter xw)
00162         {
00163             xw.WriteStartElement("Parameter");
00164             xw.WriteAttributeString("QueryName", queryName);
00165             xw.WriteAttributeString("ParameterName", name);
00166             xw.WriteAttributeString("ParameterType", type.ToString());
00167             if (tableID != -1)
00168                 xw.WriteAttributeString("TableID", tableID.ToString());
00169             if (!string.IsNullOrEmpty(relatedQueryName))
00170                 xw.WriteAttributeString("RelatedQueryName", relatedQueryName);
00171             xw.WriteAttributeString("Order", order.ToString());
00172             xw.WriteAttributeString("VisibleName", visibleName);
00173             xw.WriteEndElement();
00174         }
00175         #endregion
00176     }
00177 }